package com.woniuxy.web.back;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.woniuxy.entity.DishCategory;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Specs;
import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin(value="http://192.168.6.186:8080",allowCredentials = "true")
@RestController
@JsonIgnoreProperties("handler")
//统一前缀为admin/user
@RequestMapping("admin/user")
public class UserController_b {
    @Autowired
    private UserService userService;

    @GetMapping("pageList")
    public ResponseEntity<List<User>> list(Integer current, Integer pageSize, User user){
        if(current ==null || current == 0){
            current=1;
        }
        if(pageSize == null || pageSize == 0){
            pageSize=2;
        }
        //条件筛选
        QueryWrapper<User> queryWrapper =new QueryWrapper<>();
        if(user.getName() !=null && user.getName().trim().length() !=0){
            queryWrapper.like("name",user.getName());
        }
        if (user.getPassword() !=null && user.getPassword().trim().length() !=0){
            queryWrapper.eq("password",user.getPassword());
        }

        queryWrapper.eq("status","y");
        Page<User> page=new Page<>(current,2);
        page=userService.page(page,queryWrapper);

        return new ResponseEntity(page);
    }

    /**
     * 用户维护的展示界面
     * @author wmy
     * @return 全部用户信息
     */
    @GetMapping("list")
    public ResponseEntity<List<User>> list(){
        //获取全部用户信息
        return new ResponseEntity<List<User>>(userService.getAll());
    }

    /**
     * 根据Id删除用户
     * @param id
     * @return
     */
    @GetMapping("delete/{id}")
    public ResponseEntity delete(@PathVariable int id){
        userService.delete(id);
        return ResponseEntity.SUCCESS;
    }

    @PostMapping ("update")
    public ResponseEntity<?> update(@RequestBody User user){
        user.setStatus("y");
        userService.update(user);
        return ResponseEntity.SUCCESS;
    }

    /**
     * 根据id获取对应的类别
     * @param id
     * @return
     */
    @GetMapping("get/{id}")
    public ResponseEntity<User> get(@PathVariable int id){
        return new ResponseEntity<>(userService.getById(id));
    }
}
